/*
 * Author: Ray (qray686898@163.com)
 */
import { Message } from 'element-ui';
import { getToken, setToken, removeToken } from '@/util/auth'
import { setStore, getStore } from '@/util/store'
import { validatenull } from '@/util/validate'
import { loginByUsername, mobileLogin, getAuth, logout, register, validMsg, forgetPassword, getImaceCode } from '@/api/login'
import { GetMenu } from '@/api/menu'
import { encryption } from '@/util/util'
import md5 from 'js-md5';
const user = {
    state: {
        userInfo: getStore({ name: 'userInfo' }) || {},
        permissions: getStore({ name: 'permissions' }) || null,
        roles: getStore({ name: 'roles' }) || [],
        menu: getStore({ name: 'menu' }) || [],
        isInitMenu: getStore({ name: 'isInitMenu' }) || false,
        access_token: getStore({ name: 'access_token' }) || '',
        refresh_token: getStore({ name: 'refresh_token' }) || ''
    },
    actions: {
        //忘记密码修改
        forgetPassword ({ commit, state, dispatch }, code) {
            const _this = this
            return new Promise((resolve, reject) => {
                forgetPassword(code).then(data => {
                    resolve(data)
                    return data;
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //验证短信
        validMsg ({ commit, state, dispatch }, code) {
            const _this = this
            return new Promise((resolve, reject) => {
                validMsg(code).then(data => {
                    resolve(data)
                    return data;
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 注册
        register ({ commit, state, dispatch }, userInfo) {
            const _this = this
            return new Promise((resolve, reject) => {
                register(userInfo).then(data => {
                    resolve(data)
                    return data;
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 根据用户名登录
        LoginByUsername ({ commit, state, dispatch }, userInfo) {
            const _this = this
            return new Promise((resolve, reject) => {
                loginByUsername(userInfo.username, md5(userInfo.password), userInfo.code, userInfo.appKey).then(data => {
                    setToken(data.token)
                    commit('SET_ACCESS_TOKEN', data.token)
                    commit('SET_REFRESH_TOKEN', data.token)
                    commit('SET_USER_INFO', data)
                    commit('CLEAR_LOCK')
                    resolve(data)
                    return data;
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 获取图片验证码
        getImaceCode ({ commit, state, dispatch }, userInfo) {
            const _this = this
            return new Promise((resolve, reject) => {
                getImaceCode().then(data => {
                    resolve(data)
                    return data;
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 根据手机号登录
        LoginByPhone ({ commit, state, dispatch }, userInfo) {
            const mobile = userInfo.mobile.trim()
            return new Promise((resolve, reject) => {
                mobileLogin(mobile, userInfo.code).then(data => {
                    setToken(data.token)
                    commit('SET_ACCESS_TOKEN', data.token)
                    commit('SET_REFRESH_TOKEN', data.token)
                    commit('SET_USER_INFO', data)
                    commit('CLEAR_LOCK')
                    resolve(data)
                    return data;
                }).catch(error => {
                    reject(error)
                })
            })
        },
        GetUserInfo ({
            commit,
            state,
            dispatch
        }) {
            return new Promise((resolve, reject) => {
                getAuth().then(data => {
                    commit('SET_PERMISSIONS', data)
                    resolve(data)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 登出
        LogOut ({
            commit,
            state
        }) {
            return new Promise((resolve, reject) => {
                logout(state.access_token, state.refresh_token).then(() => {
                    // 清除菜单
                    commit('SET_MENU', [])
                    // 清除权限
                    commit('SET_PERMISSIONS', [])
                    // 清除用户信息
                    commit('SET_USER_INFO', {})
                    commit('SET_ACCESS_TOKEN', '')
                    commit('SET_REFRESH_TOKEN', '')
                    commit('SET_ROLES', [])
                    commit('DEL_ALL_TAG')
                    removeToken()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 注销session
        FedLogOut ({
            commit
        }) {
            return new Promise(resolve => {
                // 清除菜单
                commit('SET_MENU', [])
                // 清除权限
                commit('SET_PERMISSIONS', [])
                // 清除用户信息
                commit('SET_USER_INFO', {})
                commit('SET_ACCESS_TOKEN', '')
                commit('SET_REFRESH_TOKEN', '')
                commit('SET_ROLES', [])
                commit('DEL_ALL_TAG')
                removeToken()
                resolve()
            })
        },
        // 获取系统菜单
        GetMenu ({
            commit
        }) {
            return new Promise(resolve => {
                GetMenu().then((data) => {
                    commit('SET_MENU', data)
                    resolve(data)
                })
            })
        }
    },
    mutations: {
        SET_ACCESS_TOKEN: (state, access_token) => {
            state.access_token = access_token
            setStore({
                name: 'access_token',
                content: state.access_token,
                type: 'session'
            })
        },
        SET_MENU: (state, menu) => {
            state.menu = menu
            setStore({
                name: 'menu',
                content: state.menu,
                type: 'session'
            })
        },
        SET_USER_INFO: (state, userInfo) => {
            state.userInfo = userInfo
            setStore({
                name: 'userInfo',
                content: state.userInfo,
                type: 'session'
            })
        },
        SET_REFRESH_TOKEN: (state, rfToken) => {
            state.refresh_token = rfToken
            setStore({
                name: 'refresh_token',
                content: state.refresh_token,
                type: 'session'
            })
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
            setStore({
                name: 'roles',
                content: state.roles,
                type: 'session'
            })
        },
        SET_PERMISSIONS: (state, permissions) => {
            const list = {}
            if (permissions && permissions.length > 0) {
                for (let i = 0; i < permissions.length; i++) {
                    list[permissions[i]] = true
                }
                state.permissions = list
            } else {
                state.permissions = null
            }
            setStore({
                name: 'permissions',
                content: state.permissions,
                type: 'session'
            })
        }
    }
}
export default user
